#ifndef ALIEN_ATTCK_GAMEOBJECTFACTORY_H
#define ALIEN_ATTCK_GAMEOBJECTFACTORY_H

#include "GameObject.h"
#include <string>
#include <map>
#include <iostream>

struct BaseCreator
{
    virtual GameObject *createGameObject() const = 0;
    virtual ~BaseCreator() {}
};

struct GameObjectFactory
{
    static GameObjectFactory *Instance()
    {
        if (pInstance == NULL)
            pInstance = new GameObjectFactory();

        return pInstance;
    }

    bool registerType(std::string typeID, BaseCreator *pCreator)
    {
        std::map<std::string, BaseCreator *>::iterator it = m_creators.find(typeID);

        if (it != m_creators.end())
        {
            delete pCreator;
        }

        m_creators[typeID] = pCreator;
        return true;
    }

    GameObject *create(std::string typeID)
    {
        std::map<std::string, BaseCreator *>::iterator it = m_creators.find(typeID);
        if (it == m_creators.end())
        {
            std::cout << "Could not find type: " << typeID << "\n";
            return NULL;
        }

        BaseCreator *pCreator = (*it).second;
        return pCreator->createGameObject();
    }

private:
    GameObjectFactory() {}
    ~GameObjectFactory() {}

    std::map<std::string, BaseCreator *> m_creators;
    static GameObjectFactory *pInstance;
};

using TheGameObjectFactory = GameObjectFactory;

#endif // header file protect